package com.netease.nim.demo.session.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.netease.nim.demo.common.util.file.FileUtil;
import com.netease.nim.demo.file.FileIcons;
import com.netease.nimlib.sdk.msg.attachment.FileAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.shwread.android.qysw10000182.R;

/**
 * Created by hzxuwen on 2015/4/22.
 */
public class MsgViewHolderFileRight extends MsgViewHolder{
    private ImageView fileIcon;
    private TextView fileNameLabel;
    private TextView fileStatusLabel;
    private ProgressBar progressBar;
    private FileAttachment msgAttachment;

    @Override
    protected int getViewHolderDirection() {
        return VIEW_HOLDER_RIGHT;
    }

    @Override
    protected int getResId() {
        return R.layout.file_message_right_item;
    }

    @Override
    public void inflate() {
        super.inflate();
        fileIcon = (ImageView) view.findViewById(R.id.file_icon_image);
        fileNameLabel = (TextView)view.findViewById(R.id.file_name_label);
        fileStatusLabel = (TextView)view.findViewById(R.id.file_status_label);
        progressBar = (ProgressBar)view.findViewById(R.id.file_transfer_progress_bar);

        longClickView = view.findViewById(R.id.file_detail_layout);
    }

    @Override
    public void refresh(Object item) {
        super.refresh(item);
        msgAttachment = (FileAttachment) messageItem.getMessage().getAttachment();

        loadImageView();
        addDownloadListener();
    }

    private void loadImageView() {
        int iconResId = FileIcons.smallIcon(msgAttachment.getFileName());
        fileIcon.setImageResource(iconResId);
        fileNameLabel.setText(msgAttachment.getFileName());
        fileStatusLabel.setVisibility(View.VISIBLE);
        // 文件长度
        StringBuilder sb = new StringBuilder();
        sb.append(FileUtil.formatFileSize(msgAttachment.getSize()));
        fileStatusLabel.setText(sb.toString());
    }

    private void addDownloadListener() {
        if (messageItem.getMessage().getAttachStatus() == AttachStatusEnum.transferred) {
            redownload();
        }
    }

}
